// ignore_for_file: prefer_const_constructors, avoid_unnecessary_containers, avoid_print

import 'package:flutter/material.dart';

import 'package:junyeyishu/utils/utils.dart';

import 'package:junyeyishu/widgets/QGWidget.dart';

class QGModifyMickname extends StatefulWidget {
  String? nickname;
  QGModifyMickname({super.key, this.nickname});

  @override
  State<QGModifyMickname> createState() => _QGModifyMicknameState();
}

class _QGModifyMicknameState extends State<QGModifyMickname> {
  final nicknameController = TextEditingController();

  @override
  void initState() {
    super.initState();
    nicknameController.text = widget.nickname!;
  }

  @override
  Widget build(BuildContext context) {
    return Stack(children: [
      Container(
        height: 240.rpx,
        padding: EdgeInsets.fromLTRB(20.rpx, 25.rpx, 20.rpx, 0.rpx),
        child: Column(children: [
          buildTitle(),
          SizedBox(
            height: 24.rpx,
          ),
          buildNickname(),
          SizedBox(
            height: 30.rpx,
          ),
          buildButton()
        ]),
      ),
      Positioned(
          right: 15.rpx,
          top: 15.rpx,
          width: 15.rpx,
          height: 15.rpx,
          child: GestureDetector(
            onTap: () {
              Navigator.of(context).pop();
            },
            child: QGImage(asset: 'mine_nickname_close'),
          ))
    ]);
  }

  buildTitle() {
    return Container(
      child: Text(
        '修改昵称',
        style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 16.rpx,
            color: Color(0xFFffffff)),
      ),
    );
  }

  buildNickname() {
    return Row(
      children: [
        Expanded(
          child: Container(
            alignment: Alignment.center,
            height: 55.rpx,
            padding: EdgeInsets.fromLTRB(15.rpx, 0.rpx, 15.rpx, 0.rpx),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(
                  color: Color(0XFF3D4854)
                )
              ),
                borderRadius: BorderRadius.circular(6.rpx),
                color: Color(0xFF232A32)),
            child: TextField(
              textAlign:TextAlign.center,
              controller: nicknameController,
              keyboardType: TextInputType.text,
              cursorColor: Color(0xFFffffff),
              style: TextStyle(
                fontSize: 14.rpx,
                fontWeight: FontWeight.normal,
                color: Color(0xFFffffff),
              ),
              decoration: InputDecoration(
                hintText: '请输入昵称',
                hintStyle: TextStyle(color: Color(0xFF999999)),
                border: OutlineInputBorder(borderSide: BorderSide.none),
                contentPadding: EdgeInsets.zero,
                enabledBorder: InputBorder.none,
                focusedBorder: InputBorder.none,
              ),
            ),
          ),
        ),
      ],
    );
  }

  buildButton() {
    return Row(
      children: [
        Expanded(
          child: Container(
            height: 44.rpx,
            decoration: BoxDecoration(
              gradient: const LinearGradient(
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
                colors: [Color(0xFF4AE3FF), Color(0xFF44FFF0)],
              ),
              borderRadius: BorderRadius.circular(19.rpx),
            ),
            child: TextButton(
              onPressed: () {
                toModifyNickname();
              },
              style:
                  ButtonStyle(tapTargetSize: MaterialTapTargetSize.shrinkWrap),
              child: Text(
                '确定',
                style: TextStyle(
                    fontSize: 14.rpx,
                    fontWeight: FontWeight.bold,
                    color: Color(0xFF111111)),
              ),
            ),
          ),
        ),
      ],
    );
  }

  toModifyNickname() {
    var nickname = nicknameController.text;
    if (nickname.isEmpty) {
      toast('请输入昵称');
      return;
    }
    unfocus(context);

    var param = {};
    param["nickname"] = nickname;
Map<String, dynamic> stringMap = param.cast<String, dynamic>();
    Network.post(url_u_modifyNickname, data: stringMap).then((res) {
      toast(res.msg);
      if (res.success) {
        Future.delayed(Duration(seconds: 1), () {
          Navigator.of(context).pop(res.data["nickname"]);
        });
      }
    });
  }
}
